package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketTopic;
import com.powernobug.bean.model.*;
import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.TopicQueryParam;
import com.powernobug.mapper.MarketAdMapper;
import com.powernobug.mapper.MarketCouponMapper;
import com.powernobug.mapper.MarketTopicMapper;
import com.powernobug.service.AdminTopicService;
import com.powernobug.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @description:
 * @project: project1-no-bug
 * @package: com.powernobug.service
 * @author: HuangWeiLong
 * @date: 2024/8/23 14:58
 */
public class AdminTopicServiceIml implements AdminTopicService {
    @Override
    public BaseRespVo update(MarketTopic marketTopic) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);

        mapper.updateByPrimaryKey(marketTopic);
        return BaseRespVo.ok(marketTopic);
    }

    @Override
    public void delete(MarketTopic marketTopic) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        mapper.deleteByPrimaryKey(marketTopic.getId());
    }

    @Override
    public PageVo list(TopicQueryParam topicQueryParam) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        PageHelper.startPage(topicQueryParam.getPage(), topicQueryParam.getLimit());
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.setOrderByClause(topicQueryParam.getSort()+" "+topicQueryParam.getOrder());
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        if(topicQueryParam!=null){
            if(topicQueryParam.getTitle()!=null&&topicQueryParam.getTitle().length()>0){
                criteria.andTitleLike("%"+topicQueryParam.getTitle()+"%");
            }
            if(topicQueryParam.getSubtitle()!=null&&topicQueryParam.getSubtitle().length()>0){
                criteria.andSubtitleLike("%"+topicQueryParam.getSubtitle()+"%");
            }
        }
        List<MarketTopic> marketTopics = mapper.selectByExample(marketTopicExample);
        return PageVo.list(marketTopics);

    }

    @Override
    public BaseRespVo create(MarketTopic marketTopic) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        mapper.insert(marketTopic);
        return BaseRespVo.ok(marketTopic);
    }
}
